"""
CMDB Admin Configuration
"""
from django.contrib import admin
from .models import Host, HostGroup, Project, Environment, EmailConfig, AuditLog, InspectionHistory


@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
    list_display = ['name', 'description', 'created_at', 'updated_at']
    search_fields = ['name', 'description']
    list_filter = ['created_at']


@admin.register(Environment)
class EnvironmentAdmin(admin.ModelAdmin):
    list_display = ['name', 'env_type', 'description', 'created_at', 'updated_at']
    search_fields = ['name', 'description']
    list_filter = ['env_type', 'created_at']


@admin.register(HostGroup)
class HostGroupAdmin(admin.ModelAdmin):
    list_display = ['name', 'description', 'created_at', 'updated_at']
    search_fields = ['name', 'description']
    list_filter = ['created_at']


@admin.register(Host)
class HostAdmin(admin.ModelAdmin):
    list_display = ['name', 'ip', 'port', 'username', 'auth_type', 'status', 
                    'project', 'environment', 'host_group', 'created_at']
    search_fields = ['name', 'ip', 'description']
    list_filter = ['status', 'auth_type', 'project', 'environment', 'host_group', 'created_at']
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'ip', 'port', 'status', 'description')
        }),
        ('SSH认证', {
            'fields': ('username', 'auth_type', 'password', 'ssh_key')
        }),
        ('关联信息', {
            'fields': ('project', 'environment', 'host_group')
        }),
    )


@admin.register(EmailConfig)
class EmailConfigAdmin(admin.ModelAdmin):
    list_display = ['user', 'email_address', 'email_provider', 'smtp_server', 
                    'smtp_port', 'use_ssl', 'is_active', 'created_at']
    search_fields = ['user__username', 'email_address']
    list_filter = ['email_provider', 'use_ssl', 'is_active', 'created_at']
    fieldsets = (
        ('用户信息', {
            'fields': ('user',)
        }),
        ('邮箱配置', {
            'fields': ('email_provider', 'email_address', 'smtp_server', 
                      'smtp_port', 'use_ssl', 'authorization_code')
        }),
        ('状态', {
            'fields': ('is_active',)
        }),
    )


@admin.register(AuditLog)
class AuditLogAdmin(admin.ModelAdmin):
    list_display = ['timestamp', 'level', 'action', 'title', 'host_name', 'host_ip', 
                    'resource_type', 'resource_value', 'is_resolved', 'user']
    search_fields = ['title', 'message', 'host_name', 'host_ip']
    list_filter = ['level', 'action', 'resource_type', 'is_resolved', 'timestamp']
    readonly_fields = ['timestamp']
    date_hierarchy = 'timestamp'
    fieldsets = (
        ('基本信息', {
            'fields': ('timestamp', 'level', 'action', 'user')
        }),
        ('主机信息', {
            'fields': ('host', 'host_ip', 'host_name')
        }),
        ('资源信息', {
            'fields': ('resource_type', 'resource_value', 'threshold')
        }),
        ('详细信息', {
            'fields': ('title', 'message', 'extra_data')
        }),
        ('解决状态', {
            'fields': ('is_resolved', 'resolved_at', 'resolved_by')
        }),
    )


@admin.register(InspectionHistory)
class InspectionHistoryAdmin(admin.ModelAdmin):
    list_display = ['host', 'timestamp', 'cpu_usage', 'memory_usage', 'disk_usage', 
                    'status', 'has_warning']
    search_fields = ['host__name', 'host__ip']
    list_filter = ['status', 'has_warning', 'timestamp']
    readonly_fields = ['timestamp']
    date_hierarchy = 'timestamp'
    fieldsets = (
        ('基本信息', {
            'fields': ('host', 'timestamp', 'status', 'has_warning')
        }),
        ('CPU信息', {
            'fields': ('cpu_usage', 'cpu_core_count', 'cpu_frequency')
        }),
        ('内存信息', {
            'fields': ('memory_usage', 'memory_total', 'memory_used', 'memory_available')
        }),
        ('磁盘信息', {
            'fields': ('disk_usage', 'disk_total', 'disk_used', 'disk_free', 'disk_mount_point')
        }),
    )
